﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using School.Common;

namespace School.Organization
{
    public static class Staff
    {
        private static SchoolEntities db = new SchoolEntities();

        public static IQueryable<ORG_Stuff> All()
        {
            return from x in db.ORG_Stuff orderby x.FName, x.MName, x.FName select x;
            
        }

        public static IEnumerable<ORG_Role> Roles()
        {
            return from x in db.ORG_Role orderby x.Name select x;
        }

        public static ORG_Stuff Single(long id)
        {
            var data = from x in db.ORG_Stuff where x.Id == id select x;

            if (data.Count() > 0)
                return data.First();
            else
                return null;
        }

        public static ORG_Stuff GetByUserNamePassword(String UserName, String pass)
        {
            var data = from x in db.ORG_Stuff
                       where x.UserName == UserName
                       && x.UPassword == pass
                       select x;

            if (data.Count() > 0)
                return data.First();
            else
                return null;
        }

        public static long Add(ORG_Stuff staff)
        {
            db.AddToORG_Stuff(staff);
            db.SaveChanges();
            long id = staff.Id;
            return id;
        }

        public static void Update(ORG_Stuff staff)
        {
            var s = Single(staff.Id);

            s.FName = staff.FName;
            s.MName = staff.MName;
            s.LName = staff.LName;

            if (staff.Manager_Id != null)
                s.Manager_Id = staff.Manager_Id;

            s.Department_Id = staff.Department_Id;
            s.Role_Id = staff.Role_Id;
            s.LeaveDate = staff.LeaveDate;
            s.JoinDate = staff.JoinDate;
            s.PhoneNumber = staff.PhoneNumber;
            s.HomeAddress = staff.HomeAddress;
            s.IsActvie = staff.IsActvie;
            s.UserName = staff.UserName;
            db.SaveChanges();
        }


        public static bool DeleteStaff(long Id)
        {
            var deletequery = (from st in db.ORG_Stuff_Teacher
                               where st.Stuff_Id == Id
                               select st).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }
        public static bool Getstuf_byId(long id)
        {
            var qyery = (from st in db.ORG_Stuff_Teacher
                         where st.Stuff_Id == id
                         select st);


            if (qyery.Count() > 0)
                return true;
            else
                return
                    false;

        }
        public static void Add_ORG_Stuff_Teacher(ORG_Stuff_Teacher staff)
        {
            db.AddToORG_Stuff_Teacher(staff);
            db.SaveChanges();
        }
    }
}
